package com.crm.controller.system.authority;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.base.ResponseResult;
import com.crm.domain.entity.Role;
import com.crm.domain.query.RoleQuery;
import com.crm.service.system.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/role")
@RequiredArgsConstructor
public class RoleController {
    private final RoleService roleService;

    /**
     * 查询角色列表
     * @param query
     * @return
     */
    @GetMapping("/list")
    public ResponseResult list(RoleQuery query) {
        Page<Role> queryPage = roleService.queryPage(query);
        return ResponseResult.success()
                .put("total", queryPage.getTotal())
                .put("rows", queryPage.getRecords())
                .put("params",null);
    }


    /**
     * 根据id查询角色
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult getById(@PathVariable Long id){
        Role role = roleService.lambdaQuery().eq(Role::getRoleId, id).one();
        return ResponseResult.success(role);
    }

    /**
     * 删除角色
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public ResponseResult deleteById(@PathVariable List<Long> ids) {
        ids.forEach(id -> roleService.lambdaUpdate().eq(Role::getRoleId, id).remove());
        return ResponseResult.success();
    }
    /**
     * 新增角色
     * @param role
     * @return
     */
    @PostMapping
    public ResponseResult save(@RequestBody Role role) {
        roleService.save(role);
        return ResponseResult.success();
    }

}
